<?php
function log_error($num, $str, $file, $line)
{
    $message = date("Y-m-d H:i:s") . " 文件：{$file},行数：{$line},错误信息：{$str}";
    file_put_contents("../api_resource/logs/" . date("Ymd") . "_queue.log", $message . PHP_EOL, FILE_APPEND);
}
set_error_handler("log_error");

include_once '../api_resource/mysql_pdo.php';

include_once '../api_resource/functions.php';


$params = $_GET;
$act = $params['act'] ?? '';
$order_id = $params['order_id'] ?? 0;

//$skip_hook = $params['skip_hook'] ?? 0;

$db = new mysql_pdo();



$order_mobile = '';

if ($order_id > 0) {
    $order = select_table_one($db, 'cz_order', [
        'id' => intval($order_id)
    ]);
    if (!$order) {
        error('订单不存在');
    }
    $order_mobile = $order['mobile'];
    $product_id = $order['product_id'];
    $spec_id = $order['spec_id'];


    if (isset($params['product_id'])) {
        $product_id = $params['product_id'];
        $face_value = $order['face_value'];
        // 获取新产品下的规格(根据面值和产品id来)
        $spec_info = select_table_one($db, 'cz_spec', [
            'face_value' => $face_value,
            "product_id" => $product_id
        ]);
        $spec_id = $spec_info['id'];
    }
    // file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log", $spec_id . "--params--" . print_r($params, 1) . PHP_EOL, FILE_APPEND);


    $order_sn = $order['order_sn'];
    $skip_hook = $order['skip_hook'] ?? 0;
    $user = select_table_one($db, 'sys_users', [
        'id' => intval($order['create_by'])
    ]);
} else {
    error("参数有误");
}
if (in_array($act, ['queue_order', 'push_order'])) {

    if (in_array($order['order_status'], [3, 4, 5, 6])) {
        error("订单状态不支持操作[{$order['order_status']}]");
    }
    if ($order['is_auto'] == 0) {
        error('手动订单不支持此操作');
    }
}
/**推送订单队列**/
if ($act == 'queue_order') {
    $change_channel_id = $params['change_channel_id'] ?? 0;

    //todo 是否拦截
    $hook_info = check_hook($db, $order);

    $is_hook = $hook_info['hook'];
    $hook_act = $hook_info['act'];
    //跳过拦截
    if ($skip_hook == 1) {
        $is_hook = 0;
    }
    if ($is_hook == 1) {
        if ($hook_act == 1) {
            //压单
            update_table($db, 'cz_order', [
                'msg' => '[拦截压单]',
                'order_status' => 5,
                'updated_at' => time()
            ], [
                'id' => $order_id
            ]);
            success([], '拦截器处理');
        }
        if ($hook_act == 2 && $order['is_auto'] == 1) {
            //手动处理
            update_table($db, 'cz_order', [
                'msg' => '[拦截手动]',
                'is_auto' => 0,
                'updated_at' => time()
            ], [
                'id' => $order_id
            ]);
            success([], '拦截器处理');
        }
        if ($hook_act == 4) {
            //失败
            $product_info = select_table_one($db, 'cz_product', [
                'id' => $product_id,
            ]);

            $spec_info = select_table_one($db, 'cz_spec', [
                'id' => $spec_id,
            ]);
            //订单失败
            order_fail($db, '[拦截失败]', $order, $user, $product_info, $spec_info);
            success([], '拦截器处理');
        }
    }

    //todo 是否拦截
    //todo 考虑是否合并更新
    //重新编排渠道
    if ($is_hook == 1 && $hook_act == 3) {
        //换渠道产品 这里是渠道产品表的ID和次数,2022 12/11修改为返回channel_id
        $order_channel_id_nums = $hook_info['id_num'];
        $is_loop = $hook_info['is_loop'];
    } else {
        $order_channel_info = get_order_channel_ids($db, $user, [
            'product_id' => $product_id,
            'spec_id' => $spec_id,
        ], $order);

        $order_channel_id_nums = $order_channel_info['id_num']; //id-nums  代表次数
        $is_loop = $order_channel_info['is_loop'];
    }


    //重提改渠道 2022-0418
    if (count($order_channel_id_nums) <= 1 && $change_channel_id > 0) {
        //        $order_channel_id_nums = [$change_channel_id];
        $order_channel_id_nums = ["{$change_channel_id}-1"];
    }
    //重提改渠道 2022-0418
    if (count($order_channel_id_nums) > 1 && $change_channel_id > 0) {
        //        array_unshift($order_channel_id_nums, $change_channel_id);
        array_unshift($order_channel_id_nums, "{$change_channel_id}-1");
    }

    // file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log", $order_id . "--order_channel_id_nums--" . print_r($order_channel_id_nums, 1) . PHP_EOL, FILE_APPEND);
    //分配了用户渠道 但是没有配置渠道产品 2022-0418
    if (count($order_channel_id_nums) == 1) {

        $channel_id_nums_arr = explode('-', $order_channel_id_nums[0]);

        $channel_product = select_table_one($db, 'cz_channel_product', [
            'spec_id' => $spec_id,
            //            'channel_id' => $order_channel_id_nums[0],
            'channel_id' => $channel_id_nums_arr[0]
        ]);
        // file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log", $order_id . "--channel_product--" . print_r($channel_product, 1) . PHP_EOL, FILE_APPEND);
        if (!$channel_product) {
            //压单
            update_table($db, 'cz_order', [
                'msg' => '[拦截压单]未配置渠道产品' . $product_id . '|' . $spec_id,
                'order_status' => 5,
                'updated_at' => time()
            ], [
                'id' => $order_id
            ]);
            success([], '未配置渠道产品');
        }
    }

    $order_queue_data = [
        'order_id' => $order_id,
        'channel_queue' => implode(",", $order_channel_id_nums),
        'queue_status' => 1,
        'created_at' => time(),
        'updated_at' => time(),
        'is_loop' => $is_loop,
        'is_hook' => $is_hook ?? 0
    ];

    // file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log", $order_id . "--order_queue_data--" . print_r($order_queue_data, 1) . PHP_EOL, FILE_APPEND);
    // file_put_contents("../api_resource/logs/" . date("Ymd") . "_channelinfo.log", $order_id . "--order_channel_info--" . print_r($order_channel_info, 1) . PHP_EOL, FILE_APPEND);

    $order_queue = select_table_one($db, 'cz_order_queue', [
        'order_id' => intval($order_id)
    ]);
    //判断是否存在
    if ($order_queue) {
        if ($order_queue['queue_status'] == 2) {
            error('订单还在处理中..');
        }
        //存在就更新
        $order_queue_id = update_table($db, 'cz_order_queue', [
            'channel_queue' => $order_queue_data['channel_queue'],
            'queue_status' => 1,
            'updated_at' => time(),

            'is_loop' => $is_loop,
            'is_hook' => $is_hook ?? 0
        ], [
            'order_id' => $order_id
        ]);
    } else {
        $order_queue_id = insert_data_to_table($db, 'cz_order_queue', $order_queue_data);
    }

    if ($order_queue_id > 0) {
        if ($spec_id != $order['spec_id']) {
            // file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log", $order_id . "--不想等--" . print_r($order_queue_data, 1) . PHP_EOL, FILE_APPEND);

            // 换渠道产品
            sock_queue($order_id, 'push_order', [
                "product_id" => $product_id
            ]);
        } else {
            // file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log", $order_id . "--想等--" . print_r($order_queue_data, 1) . PHP_EOL, FILE_APPEND);
            sock_queue($order_id, 'push_order');
        }
    } else {
        if ($spec_id != $order['spec_id']) {
            // 换渠道产品
            sock_queue($order_id, 'queue_order', [
                "product_id" => $product_id
            ]);
        } else {
            sock_queue($order_id, 'queue_order');
        }
    }
    success([], 'ok');
}
/**推送到渠道**/
if ($act == 'push_order') {

    $order_queue = select_table_one($db, 'cz_order_queue', [
        'order_id' => intval($order_id)
    ]);

    // 20200527 存在订单(手机号),黑名单处理
    if ($order_mobile) {
        $is_blank = select_table_one($db, 'cz_mobile_blank', [
            'mobile' => $order_mobile
        ]);
        if ($is_blank) {
            // error('黑名单号码');
            // 修改订单为失败
            //更新队列状态为完成
            update_queue_order_status($db, $order_id, 3);

            $product_info = select_table_one($db, 'cz_product', [
                'id' => $product_id,
            ]);

            $spec_info = select_table_one($db, 'cz_spec', [
                'id' => $spec_id,
            ]);
            $msg = "[失败QD]黑名单号码";
            //订单失败
            order_fail($db, $msg, $order, $user, $product_info, $spec_info);
            error('黑名单号码');
        }
    }


    if (!$order_queue) {
        error('订单队列不存在');
    }

    if (in_array($order['order_status'], [3, 4])) {
        error('订单已完成。');
    }

    if ($order_queue['queue_status'] == 2) {
        error('队列正在处理中');
    }
    if ($order_queue['queue_status'] == 3) {
        error('订单已处理完成');
    }
    //检查渠道订单是否有处理中的
    $channel_order = select_table_one($db, 'cz_channel_order', [
        'order_id' => intval($order_id),
        'channel_order_status' => ['IN', '(1,2)']
    ]);
    if ($channel_order) {
        error('有渠道订单正在处理');
    }
    $order_queue_nums = $order_queue['order_nums'] + 1;
    $channel_ids = explode(",", $order_queue['channel_queue']);
    //获取第一个渠道id并删除
    $first_channel_id_info = array_shift($channel_ids);
    $channel_id_nums_arr = explode('-', $first_channel_id_info);
    //    var_dump($channel_id_nums_arr);exit;
    $channel_id = $channel_id_nums_arr[0];
    $channel_nums = $channel_id_nums_arr[1] ?? 0;

    //重提次数的就重新提
    if ($channel_nums > 1) {
        $channel_nums = $channel_nums - 1;
        array_unshift($channel_ids, "{$channel_id}-{$channel_nums}");
    }

    //todo 以哪个循环判断为准 $order_queue['is_loop']
    if (!$channel_id && $order['is_loop'] == 0) {
        // file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log",   "没有渠道不是无限重提" . $order_id . PHP_EOL, FILE_APPEND);
        //更新队列状态为完成
        update_queue_order_status($db, $order_id, 3);

        $product_info = select_table_one($db, 'cz_product', [
            'id' => $product_id,
        ]);

        $spec_info = select_table_one($db, 'cz_spec', [
            'id' => $spec_id,
        ]);

        $msg = "[失败QD-null]";
        //订单失败
        order_fail($db, $msg, $order, $user, $product_info, $spec_info);

        error('渠道跑完了');
    }

    if ($channel_id) {
        //更新状态以及渠道id
        $is_update = update_table($db, 'cz_order_queue', [
            'queue_status' => 2,
            'channel_queue' => implode(",", $channel_ids),
            'order_nums' => ['=', 'order_nums+1'],
            'updated_at' => time()
        ], [
            'queue_status' => 1,
            'order_id' => $order_id
        ]);


        if ($order_queue['is_hook'] == 1) {
            //20220104 如果是拦截 ID是渠道产品的ID 不是渠道ID(2022/12/11修改为channel_id)
            // $channel_product_id = $channel_id;
            // //获取渠道产品信息
            // $channel_product = select_table_one($db, 'cz_channel_product', [
            //     'id' => $channel_product_id
            // ]);
            // $channel_id = $channel_product['channel_id'];
            // 1todo 2022 12 11 是拦截产品也是取channel_id
            $channel_product = select_table_one($db, 'cz_channel_product', [
                'spec_id' => $spec_id,
                'channel_id' => $channel_id
            ]);
        } else {
            //获取通道信息
            $channel_product = select_table_one($db, 'cz_channel_product', [
                'spec_id' => $spec_id,
                'channel_id' => $channel_id
            ]);
        }
        // file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log",  "channel_product----"  . print_r($channel_product, 1) . PHP_EOL, FILE_APPEND);


        $channel_obj = get_channel_obj($channel_id, $db);


        //没有渠道产品或已下架就继续处理
        if (!$channel_product || $channel_product['status'] == 0) {
            //更新状态为1待处理 继续推送
            update_queue_order_status($db, $order_id, 1);
            sock_queue($order_id, 'push_order');
            error('渠道产品不存在或已下架');
        }

        //渠道订单入库
        $channel_order_data = [
            'order_id' => $order_id,
            'order_sn' => $order['order_sn'],
            'channel_order_status' => 1,
            'product_id' => $order['product_id'],
            'spec_id' => $order['spec_id'],
            'channel_id' => $channel_id,
            'channel_api_code' => $channel_product['channel_api_code'],
            'created_at' => time(),
            'updated_at' => time(),
            'user_id' => $order['create_by'],
            'channel_price' => $channel_product['channel_price'],
        ];

        $channel_order_id = insert_data_to_table($db, 'cz_channel_order', $channel_order_data);
        if (!$channel_order_id) {
            //更新状态为1待处理 继续推送
            update_queue_order_status($db, $order_id, 1);
            sock_queue($order_id, 'push_order');
            error('渠道订单入库失败');
        }
        //推送给渠道的订单号
        $push_order_sn = $order['order_sn'] . "C" . sprintf('%04d', $order_queue_nums);
        //更新客户订单为处理中
        update_cz_order_status($db, $order_id, 2, '充值中[' . $channel_order_id . ']');
        //提交订单到渠道
        $submit_params = [
            'push_order_sn' => $push_order_sn,
        ];
        $submit_params = array_merge($order, $submit_params);

        // var_dump($submit_params);
        // file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log",  "submit_params----" . $channel_product['channel_api_code'] . print_r($submit_params, 1) . PHP_EOL, FILE_APPEND);

        $result = $channel_obj->submitOrder($order['mobile'], $channel_product['channel_api_code'], $submit_params);
        file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log",  "result----" . print_r($result, 1) . PHP_EOL, FILE_APPEND);

        //记录提交到上游的提交次数
        $order['cur_submit_order_count']++;
        update_table($db, 'cz_order', [
            'cur_submit_order_count' => $order['cur_submit_order_count'],
        ], [
            'id' => $order_id
        ]);
        $update_params = [
            'push_order_sn' => $push_order_sn,
            'msg' =>  substr($result['msg'], 0, 100),
            'post_data' => $result['post_data'],
            'channel_data' => $result['channel_data'] ?? '返回空',
            'updated_at' => time()
        ];

        if ($result['error'] == 0) {

            $update_params['channel_order_status'] = 2;
            $update_params['channel_order_sn'] = $result['channel_order'];

            //更新成本
            $update_params['channel_price'] = $result['channel_order_price'] ?? $channel_product['channel_price'];

            //更新渠道订单信息
            update_table($db, 'cz_channel_order', $update_params, [
                'id' => $channel_order_id
            ]);

            //更新客户订单成本以及channel_id
            update_table($db, 'cz_order', [
                'channel_id' => $channel_id,
                'cost' => $update_params['channel_price']
            ], [
                'id' => $order_id
            ]);
            // 处理流量直充 充值中需要等待回调，这里处理直接返回结果
            if ($order['biz_type'] == 2) {
                // 流量充值
                $order_status = $result['order_status'] ?? -1;
                if ($order_status == 3) {
                    // 成功
                    // 更新
                    $channel_order_data['id'] = $channel_order_id;
                    channel_callback_success($db, $channel_order_data, $order, $result['msg']);
                } else if ($order_status == 4) {
                    // 失败
                    $channel_order_data['id'] = $channel_order_id;
                    // $channel_order_data['order_sn'] = $update_params['channel_order_sn'];
                    channel_callback_fail($db, $channel_order_data, $cz_order, $msg);
                }
            }
            success([], $result['msg']);
        } else {
            if (isset($result['error'])) {

                $update_params['channel_order_status'] = 4;
                //更新渠道订单信息
                update_table($db, 'cz_channel_order', $update_params, [
                    'id' => $channel_order_id
                ]);

                //更新状态重新入队列
                update_queue_order_status($db, $order_id, 1);

                //余额不足 压单
                if ($result['code'] === 2000) {
                    //更改状态为压单
                    update_cz_order_status($db, $order_id, 6, substr("[拦截]渠道余额不足", 0, 100));
                } else {
                    file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log",   "提交上游失败" . $order_id . '提交返回数据' . print_r($result, 1) . PHP_EOL, FILE_APPEND);
                    //todo 20220616
                    // sleep(5);
                    sock_queue($order_id, 'push_order');
                }
                error($result['msg']);
            }
            //todo 是否要去检查渠道订单再重新进入

        }
    } else {
        // 没有渠道且是无限重提  重新修改 渠道订单
        // 判断是否超时和终止
        $cz_order_timeout = $order['timeout'];
        $is_time_out = $cz_order_timeout > 0 && ($cz_order_timeout - time()) < 0;
        // file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log",   "is_stop" . $order['is_stop'] . "is_time_out" . $is_time_out . PHP_EOL, FILE_APPEND);
        if ($is_time_out || $order['is_stop'] == 1) {
            //更新队列状态为完成
            update_queue_order_status($db, $order_id, 3);

            $product_info = select_table_one($db, 'cz_product', [
                'id' => $product_id,
            ]);

            $spec_info = select_table_one($db, 'cz_spec', [
                'id' => $spec_id,
            ]);
            //超时订单不进行拦截
            $product_info['stop_fail'] = 0;
            //订单失败
            $msg = '重提队列（queue），充值失败';   //todo  出现这种情况是没有回调一提交就失败（余额不足，编码失败，产品下架，产品维护等）
            // if($order['status'])
            order_fail($db,  substr(($order['is_stop'] ? "[终止]" : "[超时]") . $msg, 0, 100), $order, $user, $product_info, $spec_info);
            success([], substr(($order['is_stop'] ? "[终止]" : "[超时]") . $msg, 0, 100));
        } else {
            // 重新推送队列
            // file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log",   "没有渠道是无限重提" . $order_id . PHP_EOL, FILE_APPEND);
            // file_put_contents("../api_resource/logs/" . date("Ymd") . "_queuedata.log",   "重新推送到队列" . print_r($order, 1) . PHP_EOL, FILE_APPEND);
            //添加提交次数限制
            if ($order['cur_submit_order_count'] >= $order['submit_order_count']) {
                //提交次数上限
                $msg = "充值失败[提交次数达到上限-{$order['submit_order_count']}]";
                order_fail($db, $msg, $order, $user, $product_info, $spec_info);
                success([],  $msg);
            }

            update_queue_order_status($db, $order_id, 1);
            sock_queue($order_id, 'queue_order');
            success([], "重新推送到队列");
        }
    }
}
/**回调客户*/
if ($act == 'callback_user_order') {
    if (!in_array($order['order_status'], [3, 4])) {
        error("order_status值不对");
    }
    $api_secret = $user['api_secret'];
    $callback_url = $order['callback_url'];
    // todo决定是否返回pz
    $voucher = '';
    if ($user['is_voucher']) {
        $voucher = $order['voucher'] ?? '';
    }
    if ($callback_url) {
        status_call_back($api_secret, $callback_url, [
            'order_sn' => $order['order_sn'],
            'user_order_sn' => $order['user_order_sn'],
            'order_status' => $order['order_status'],
            'msg' => $order['msg'],
            'voucher' => $voucher
        ]);
    }
    success([], '[callback_user_order]回调成功');
}
/**订单退款**/
if ($act == 'order_refund') {
    if ($order['order_status'] != 4) {
        error("订单状态不支持操作[{$order['order_status']}]");
    }
    order_refund($db, $order, $user, '');
    success([], '[order_refund]操作成功');
}
/**手动修改渠道订单*/
if ($act == 'change_channel_order') {

    $push_order_sn = $params['push_order_sn'] ?? '';
    $channel_id = $params['channel_id'] ?? 0;
    $channel_order_status = $params['channel_order_status'] ?? 0;

    $cz_order = $order;

    $channel_order = select_table_one($db, 'cz_channel_order', [
        'push_order_sn' => $push_order_sn
    ]);
    if (!$channel_order) {
        error('渠道订单不存在[QD]');
    }
    /*
    if(in_array( intval($channel_order['channel_order_status']), [3,4] )){
        error('订单已回调');
    }
    */
    //匹配订单是否此渠道的
    if ($channel_order['channel_id'] != $channel_id) {
        error('回调渠道不匹配');
    }

    if (in_array(intval($cz_order['order_status']), [3, 4])) {
        error('订单结果已完成');
    }
    $msg = $channel_order['msg'];
    $voucher = $cz_order['voucher'] ?? '';
    if ($channel_order_status == 3) {
        channel_callback_success($db, $channel_order, $cz_order, $msg, $voucher);
        success([], '已操作回调成功');
    }
    if ($channel_order_status == 4) {
        channel_callback_fail($db, $channel_order, $cz_order, $msg);
        success([], '已操作回调失败');
    }
    error('状态有误');
}
if ($act == 'check_channel_order') {
    $push_order_sn = $params['push_order_sn'] ?? '';
    $cz_order = $order;

    $channel_order = select_table_one($db, 'cz_channel_order', [
        'push_order_sn' => $push_order_sn
    ]);
    if (!$channel_order) {
        error('渠道订单不存在[QD]');
    }
    if (in_array(intval($channel_order['channel_order_status']), [3, 4])) {
        error('订单已回调');
    }
    //20220504 add 如果是订单已有结果的 不在进行处理
    if (in_array(intval($cz_order['order_status']), [3, 4])) {
        error('订单状态不允许');
    }
    //20220521 add 非自动订单不进行处理
    if (intval($cz_order['is_auto']) != 1) {
        error('订单处理方式不允许');
    }

    $msg = '[接口查询状态]';

    $channel_obj = get_channel_obj($channel_order['channel_id'], $db);
    if (method_exists($channel_obj, 'queryOrder')) {
        $resp = $channel_obj->queryOrder($channel_order);
        //        var_dump($resp);
        $msg = $msg . ($resp['msg'] ?? '');
        $voucher = $resp['voucher'] ?? '';
        if ($resp['order_status'] == 3) {
            channel_callback_success($db, $channel_order, $cz_order, $msg, $voucher);
            success([], '已操作回调成功');
        }
        if ($resp['order_status'] == 4) {
            channel_callback_fail($db, $channel_order, $cz_order, $msg);
            success([], '已操作回调失败');
        }
    } else {
        error('方法不存在');
    }
    error('状态有误:' . $msg);
}
if ($act == 'async_customer_order') {
    $push_order_sn = $params['push_order_sn'] ?? '';
    $cz_order = $order;

    $channel_order = select_table_one($db, 'cz_channel_order', [
        'push_order_sn' => $push_order_sn
    ]);
    if (!$channel_order) {
        error('渠道订单不存在[QD]');
    }
    if (in_array(intval($channel_order['channel_order_status']), [3, 4])) {
        error('订单已回调');   //回调的渠道订单不中断执行，去同步客户订单
    }
    //20220504 add 如果是订单已有结果的 不在进行处理
    if (in_array(intval($cz_order['order_status']), [3, 4])) {
        error('订单状态不允许');
    }
    //20220521 add 非自动订单不进行处理
    if (intval($cz_order['is_auto']) != 1) {
        error('订单处理方式不允许');
    }
    // //获取充值的手机号
    // $cz_order = select_table_one($db, 'cz_order', [
    //     'id' => $channel_order['order_id']
    // ]);
    $msg = '[接口查询状态]';

    $channel_obj = get_channel_obj($channel_order['channel_id'], $db);
    if (method_exists($channel_obj, 'queryOrder')) {
        $channel_order['mobile'] = $cz_order['mobile'];
        $resp = $channel_obj->queryOrder($channel_order);
        //        var_dump($resp);
        $msg = $msg . ($resp['msg'] ?? '');
        $voucher = $resp['voucher'] ?? '';
        if ($resp['order_status'] == 3) {
            channel_callback_success($db, $channel_order, $cz_order, $msg, $voucher);
            success([], '已操作回调成功');
        }
        if ($resp['order_status'] == 4) {
            channel_callback_fail($db, $channel_order, $cz_order, $msg);
            success([], '已操作回调失败');
        }
    } else {
        error('方法不存在');
    }
    error('状态有误:' . $msg);
}
// 查单2   2022-06-23
if ($act == 'async_customer_order_2') {
    $push_order_sn = $params['push_order_sn'] ?? '';
    $cz_order = $order;

    $channel_order = select_table_one($db, 'cz_channel_order', [
        'push_order_sn' => $push_order_sn
    ]);
    if (!$channel_order) {
        error('渠道订单不存在[QD]');
    }
    if (in_array(intval($channel_order['channel_order_status']), [3, 4])) {
        // error('订单已回调');   回调的渠道订单不中断执行，去同步客户订单
    }
    //20220504 add 如果是订单已有结果的 不在进行处理
    if (in_array(intval($cz_order['order_status']), [3, 4])) {
        // error('订单状态不允许');
    }
    //20220521 add 非自动订单不进行处理
    if (intval($cz_order['is_auto']) != 1) {
        error('订单处理方式不允许');
    }

    $msg = '[接口查询状态]';

    $channel_obj = get_channel_obj($channel_order['channel_id'], $db);
    if (method_exists($channel_obj, 'queryOrder')) {
        $resp = $channel_obj->queryOrder($channel_order);
        //        var_dump($resp);
        $msg = $msg . ($resp['msg'] ?? '');
        $voucher = $resp['voucher'] ?? '';

        if ($resp['order_status'] == 3) {
            channel_callback_success($db, $channel_order, $cz_order, $msg, $voucher);
            success([], '已操作回调成功');
        }
        if ($resp['order_status'] == 4) {
            channel_callback_fail($db, $channel_order, $cz_order, $msg);
            success([], '已操作回调失败');
        }
    } else {
        error('方法不存在');
    }
    error('状态有误:' . $msg);
}
error('未知请求操作');
